                 /* LPC13xx definitions               */
#include "interrupcoes.h"
#include "rotary.h"
#include "gpio.h"
#include "mcu_regs.h"

extern bool executando;
extern osThreadId tid_ProcessaDados;           
extern osThreadId tid_Plot; 
extern bool limpaTela;
extern uint8_t escala;

void PIO0_IRQHandler(void)
{
  uint32_t regVal;
  
  regVal = GPIOIntStatus( PORT0, 1 );
  if ( regVal )
  {
	EventoBotao();
	GPIOIntClear( PORT0, 1 );
  }		
  return;
}

void PIO1_IRQHandler(void)
{
  uint32_t regVal;
  
  regVal = GPIOIntStatus( PORT1, 0 );
  if ( regVal )
  {
	EventoEncoder();
	GPIOIntClear( PORT1, 0 );
  }
  regVal = GPIOIntStatus( PORT1, 1 );
  if ( regVal )
  {
	EventoEncoder();
	GPIOIntClear( PORT1, 1 );
  }
  return;
}

void EventoEncoder(void)
{
   uint8_t evento;
   
   if(!executando) return;
   
   evento = rotary_read();
   
   switch(evento)
   {
    case ROTARY_LEFT:
      if( escala > 1)
        escala--;
      else return;
      break;
    case ROTARY_RIGHT:
      if( escala < 3)
        escala++;
      else return;
      break;
    case ROTARY_WAIT:
      return;
   }
   osSignalSet(tid_ProcessaDados, FLAG_REINICIALIZA);
   limpaTela = true;
}
void EventoBotao(void)
{
  if(!executando)
  {
      osSignalSet(tid_ProcessaDados, FLAG_EXECUTANDO);
      osDelay(10);
  }
  osSignalSet(tid_ProcessaDados, FLAG_REINICIALIZA);
  limpaTela = true;
}


